//将时间格式为 ‘2022-07-21T06:00:22.000+0000’转化为’yyyy-MM-dd HH:mm:ss’或者’yyyy/MM/dd HH:mm:ss’
const formatDateTime = (oldDate) => {
  // 方式1 转换为'yyyy-MM-dd HH:mm:ss'
  function add0(num) {
    return num < 10 ? "0" + num : num;
  } // 个位数的值在前面补0
  const date = new Date(oldDate);
  const Y = date.getFullYear();
  const M = date.getMonth() + 1;
  const D = date.getDate();
  const h = date.getHours();
  const m = date.getMinutes();
  const s = date.getSeconds();

  const dateString =
    Y +
    "-" +
    add0(M) +
    "-" +
    add0(D) +
    "  " +
    add0(h) +
    ":" +
    add0(m) +
    ":" +
    add0(s);
  return dateString;
};
//获取当天日期
const getToDay = () => {
  const today = new Date()
  const year = today.getFullYear()
  const month = String(today.getMonth() + 1).padStart(2, '0') // 月份从 0 开始，因此要加 1
  const day = String(today.getDate()).padStart(2, '0')

  return `${year}-${month}-${day}`
}
//获取当前时间
const getNowTime = () => {
  // 方式1 转换为'yyyy-MM-dd HH:mm:ss'
  function add0(num) {
    return num < 10 ? "0" + num : num;
  } // 个位数的值在前面补0
  const date = new Date();
  const Y = date.getFullYear();
  const M = date.getMonth() + 1;
  const D = date.getDate();
  const h = date.getHours();
  const m = date.getMinutes();
  const s = date.getSeconds();

  const dateString =
    Y +
    "-" +
    add0(M) +
    "-" +
    add0(D) +
    "  " +
    add0(h) +
    ":" +
    add0(m) +
    ":" +
    add0(s);
  return dateString;
}
//将文件解析成base64或buffer
function fileParse(file, type = 'base64') {
    // 根据类型读取文件
  if (type === 'base64') {
      return file.toString("base64");
    } else if (type === 'buffer') {
      return Buffer.from(file, "base64");
    } else {
      return new Error("Unsupported type: " + type);
    }
}

module.exports = {
  formatDateTime,
  getToDay,
  getNowTime,
  fileParse,
};